package com.kexun.lxj;

public class Demo03 {
    public static void main(String[] args) {
        Thread t1=new Ticket4();
        Thread t2=new Ticket4();
        Thread t3=new Ticket4();
        Thread t4=new Ticket4();

        t1.start();
        t2.start();
        t3.start();
        t4.start();

        System.out.println(t1);
    }

}


class Ticket4 extends Thread {

    private static int i = 20;

    @Override
    public void run() {
        while (true) {


            // sellTicket();
            sellTicket2();

            if(i==0){
                break;
            }
        }
    }

    public  void sellTicket() {

        synchronized(this){
            System.out.println(this);
            if (i > 0) {
                try {
                    System.out.println("这是" + Thread.currentThread().getName() + "卖出的第" + i-- + "张票");
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println(Thread.currentThread().getName() + "卖完了,关闭卖票窗口");

            }
        }

    }


    public synchronized void sellTicket2() {
            System.out.println(this);
            if (i > 0) {
                try {
                    System.out.println("这是" + Thread.currentThread().getName() + "卖出的第" + i-- + "张票");
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println(Thread.currentThread().getName() + "卖完了,关闭卖票窗口");

            }
        }


}